<div class="clear mainbox-title-container">

	<div><?php
	$user_type = @$_GET['user_type'] ? $_GET['user_type'] : @$model->user_type;
	if ( !empty($user_type) ) {
		switch ($user_type) {
			case 'A':
				echo CHtml::link('&laquo; '.t('Back').':&nbsp;'.t('Administrators'), array('/user/manage', 'user_type' => 'A'), array('class' => 'back-link strong'));
				break;
			case 'B':
				echo CHtml::link('&laquo; '.t('Back').':&nbsp;'.t('Content Providers'), array('/user/manage', 'user_type' => 'B'), array('class' => 'back-link strong'));
				break;
			case 'F':
				echo CHtml::link('&laquo; '.t('Back').':&nbsp;'.t('Cộng tác viên'), array('/user/manageCTV'), array('class' => 'back-link strong'));
				break;
			case 'C':
			default:
				echo CHtml::link('&laquo; '.t('Back').':&nbsp;'.t('Customers'), array('/user/manage', 'user_type' => 'C'), array('class' => 'back-link strong'));
				break;
		}
	} ?></div>

	<h1 class="mainbox-title">
		<?php if ( $model->user_id ) {
			echo t('Edit User Information');
		} else {
			echo t('Add User');
		} ?></h1>

	<div class="mainbox-title-bg">&nbsp;</div>
</div>
 
<?php Yii::app()->clientScript->registerScriptFile(bu() .'/js/tabs.js'); ?>

<?php
$tabs = array(
	'general' => t('General Information'),
);
if ( !empty($model->user_id) && ($model->user_type == 'B' || (isset($_GET['user_type']) && $_GET['user_type'] == 'B')) ) {
	$tabs['cp_content'] = t('Content Provider');
}
if ( !empty($model->user_id) && ($model->user_type == 'F' || (isset($_GET['user_type']) && $_GET['user_type'] == 'F')) ) {
	$tabs['ctv_content'] = t('Assign Books');
}
if ( !empty($model->user_id) && ($model->user_type == 'A' || (isset($_GET['user_type']) && $_GET['user_type'] == 'A')) ) {
	$tabs['usergroups'] = t('Usergroups');
}

$this->renderPartial('//common/tabs_header', array(
	'tabs' => $tabs,
	'active_tab' => getParam('selected_section') ? getParam('selected_section') : 'general',
)) ?>

<?php $form = $this->beginWidget('CActiveForm', array(
	'id' => 'user_form',
	'enableClientValidation' => true,
	'focus' => array($model, 'email'),
	'htmlOptions' => array(
		'class' => 'form',
		'enctype' => 'multipart/form-data',
	),
)); ?>

<div class="cm-tabs-content">

	<input type="hidden" name="selected_section" id="selected_section" value="general">

	<?php echo CHtml::errorSummary($model); ?>

	<div id="content_general" style="display: block;">
		<fieldset>
			<h2 class="subheader"><?php echo t('Account Information') ?></h2>

			<div class="form-field row">
				<?php echo $form->labelEx($model, 'email'); ?>
				<?php echo $form->textField($model, 'email', array('size' => 32, 'maxlength' => 128, 'class' => 'input-text')); ?>
				<?php echo $form->error($model, 'email'); ?>
			</div>

			<div class="form-field row">
				<?php echo $form->labelEx($model, 'password1'); ?>
				<?php echo $form->passwordField($model, 'password1', array('size' => 32, 'maxlength' => 32, 'class' => 'input-text', 'autocomplete' => 'off')); ?>
				<?php echo $form->error($model, 'password1'); ?>
			</div>

			<div class="form-field row">
				<?php echo $form->labelEx($model, 'password2'); ?>
				<?php echo $form->passwordField($model, 'password2', array('size' => 32, 'maxlength' => 32, 'class' => 'input-text', 'autocomplete' => 'off')); ?>
				<?php echo $form->error($model, 'password2'); ?>
			</div>

			<div class="form-field row">
				<?php echo $form->labelEx($model, 'status'); ?>
				<div class="select-field">
					<?php echo $form->radioButtonList($model, 'status', array(
						'A' => t('Active'),
						'D' => t('Unactive'),
					), array(
						'separator' => '&nbsp;&nbsp;',
						'class' => 'radio',
					)); ?>
				</div>
			</div>

			<div class="form-field row">
				<?php echo $form->labelEx($model, 'user_type'); ?>
				<?php echo $form->dropDownList($model, 'user_type', User::getUserTypes()) ?>
				<?php echo $form->error($model, 'user_type'); ?>
			</div>
			
			<div class="form-field row">
				<?php //echo $form->labelEx($model->main_image, 'main_image'); ?>
				<?php echo CHtml::label(t('Avatar'), '', array()); ?>
				<?php $this->renderPartial('//common/attach_images', array(
					'name' => 'user_main',
					'object_id' => $model->user_id,
					'object_type' => 'user',
					'pair' => $model->avatar,
					'delete_pair' => false,
				)); ?>
			</div>

		</fieldset>

		<fieldset>
			<h2 class="subheader"><?php echo t('Contact Information') ?></h2>

			<div class="form-field row">
				<?php echo $form->labelEx($model, 'name'); ?>
				<?php echo $form->textField($model, 'name', array('size' => 32, 'maxlength' => 256, 'class' => 'input-text')); ?>
				<?php echo $form->error($model, 'name'); ?>
			</div> 
		</fieldset>

	</div>

	<?php if ( !empty($model->user_id) && ($model->user_type == 'B' || (isset($_GET['user_type']) && $_GET['user_type'] == 'B')) ) : ?>
	<div id="content_cp_content" class="" style="display: none;">
		<fieldset>
<!--			<h2 class="subheader"><?php echo t('Default Commission') ?></h2>

			<div class="form-field row">
				<?php echo $form->labelEx($model, 'commission'); ?>
				<?php echo $form->textField($model, 'commission', array('size' => 10, 'maxlength' => 10, 'class' => 'input-text', 'defaultValue' => '20.00')); ?> %
				<?php echo $form->error($model, 'commission'); ?>
			</div>-->

			<h2 class="subheader"><?php echo t('Select Products') ?></h2>
			<?php Yii::app()->clientScript->registerScriptFile(bu() .'/js/picker.js'); ?>

			<div class="buttons-container">
				<a class="cm-external-click text-button text-button-add" rev="opener_picker_added_products_<?php echo $model->user_id ?>">Thêm sản phẩm</a>
				<div class="hidden"><a class="text-button text-button-add cm-dialog-opener" rev="content_added_products_<?php echo $model->user_id ?>" href="<?php echo $this->createUrl('/product/picker', array('data_id' => 'added_products_'.$model->user_id)) ?>" id="opener_picker_added_products_<?php echo $model->user_id ?>">Thêm</a></div>
				<div class="hidden" id="content_added_products_<?php echo $model->user_id ?>" title="<?php echo t('Add Products') ?>"></div
			</div>

			<table class="table" width="100%" cellpadding="0" cellspacing="0" border="0">
				<thead>
					<tr>
						<th width="60%"><?php echo t('Product') ?></th>
						<th width="10%"><?php echo t('Price') ?></th>
						<th width="10%"><?php echo t('Commission') ?></th>
						<th width="1%">&nbsp;</th>
					</tr>
				</thead>
				<tbody id="added_products_<?php echo $model->user_id ?>">
					<tr class="cm-js-item cm-clone hidden">
						<td><a target="_blank" href="<?php echo $this->createUrl('/product/update') ?>&id={delete_id}"><strong>{product}</strong></a>
							<input type="hidden" name="cpProducts[]" value="{delete_id}" />
						</td>
						<td>{price}</td>
						<td>{commission}%</td>
						<td class="nowrap">
							<ul class="cm-tools-list tools-list">
								<li><a class="delete" onclick="jQuery.delete_js_item('added_products_<?php echo $model->user_id ?>', '{delete_id}', 'p'); return false;">Xóa</a></li>
							</ul>
						</td>
					</tr>

					<?php if ( !empty($model->cpProducts) ) : ?>
						<?php foreach ($model->cpProducts as $p) : ?>
						<tr id="added_products_<?php echo $model->user_id ?>_<?php echo $p->product_id ?>">
							<td><a target="_blank" href="<?php echo $this->createUrl('/product/update', array('id' => $p->product_id)) ?>"><strong><?php echo $p->description->name; ?></strong></a>
								<input type="hidden" name="cpProducts[]" value="<?php echo $p->product_id; ?>" />
							</td>
							<td><?php $this->renderPartial('//common/price', array('value' => $p->price)) ?></td>
							<td><?php echo number_format($p->commission) ?>%</td>
							<td class="nowrap">
								<ul class="cm-tools-list tools-list">
									<li><a class="delete" onclick="jQuery.delete_js_item('added_products_<?php echo $model->user_id ?>', '<?php echo $p->product_id ?>', 'p'); return false;">Xóa</a></li>
								</ul>
							</td>
						</tr>
						<?php endforeach; ?>
					<?php else: ?>
						<tr><td colspan="5"><span class="empty"><?php echo t('No results found.') ?></span></td></tr>
					<?php endif; ?>
				</tbody>
			</table>

		</fieldset>
	</div>
	<?php endif; ?>

	<?php if ( !empty($model->user_id) && ($model->user_type == 'F' || (isset($_GET['user_type']) && $_GET['user_type'] == 'F')) ) : ?>
	<div id="content_ctv_content" class="" style="display: none;">
		<fieldset>
			<h2 class="subheader"><?php echo t('Assign Books') ?></h2>
			<?php
			if ( $model->user_id ) {
				$this->renderPartial('//picker/products_picker', array(
					'object_id' => $model->user_id,
					'item_name' => 'CTVBooks',
					'items' => $model->ctvProducts,
				));
			} ?>

		</fieldset>
	</div>
	<?php endif; ?>

	<?php if ( !empty($model->user_id) && ($model->user_type == 'A' || (isset($_GET['user_type']) && $_GET['user_type'] == 'A')) ) : ?>
	<div id="content_usergroups" class="" style="display: none;">
		<?php
		$this->widget('ext.EGridView', array
		(
			'id' => 'usergroups-list',
			'cssFile' => false,
			'dataProvider' => $usergroupsDataProvider,
			'enablePagination' => false,
			//'filter' => $users,
			'columns' => array (
				array (
					'header' => t('Usergroups'),
					'headerHtmlOptions' => array('width' => '50%'),
					'name' => 'usergroup',
					'htmlOptions' => array(),
					'type' => 'raw',
					'value' => 'CHtml::link($data->usergroup, array("user/updateGroup", "id" => $data->usergroup_id))',
					'sortable' => false,
				),
				array (
					'header' => t('Status'),
					'headerHtmlOptions' => array('width' => '50%'),
					'name' => 'status',
					'htmlOptions' => array(),
					'type' => 'raw',
					'value' => 'CHtml::checkBox("user_usergroups[]", in_array($data->usergroup_id, array('.implode(',', $model->usergroup_ids).')), array("uncheckValue" => null, "value" => $data->usergroup_id)) . "&nbsp;&nbsp;" . CHtml::label("'.t('Active').'", "user_usergroups_".$data->usergroup_id);',
					//'value' => 'in_array($data->usergroup_id, array('.implode(',', $model->usergroup_ids).')) ? "'.t('Active').'" : "'.t('Unactive').'"',
					'sortable' => false,
				),
			),
		));
		?>
	</div>
	<?php endif; ?>

	<div class="buttons-container buttons-bg cm-toggle-button">
		<span class="submit-button cm-button-main ">
			<?php echo CHtml::submitButton($model->isNewRecord ? t('Create') : t('Save')); ?>
		</span>
		&nbsp;<?php echo t('or') ?>&nbsp;&nbsp;
		<?php echo CHtml::link(t('Cancel'), array('/user/manage', 'user_type' => $model->user_type), array('class' => 'underlined tool-link')) ?>
	</div>

</div>

<?php $this->endWidget(); ?>